package xjh.him.activity;

import android.app.Activity;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.xjh.him.R;

import java.util.ArrayList;
import java.util.List;

import xjh.him.adapter.NewFriendAdapter;
import xjh.him.bean.NewFriendBean;
import xjh.him.common.SharedData;
import xjh.him.netty.codec.Packet;
import xjh.him.netty.dto.FriendDto;

public class NewFriendActivity extends Activity {
    private List<NewFriendBean> beans;
    private RecyclerView newFriendList;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_friend);

        initViewContent();
    }

    public void initViewContent() {
        Toolbar toolbar = findViewById(R.id.new_friend_toolbar);
        newFriendList = findViewById(R.id.new_friend_list);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        newFriendList.setLayoutManager(layoutManager);

        beans = new ArrayList<>();
        FriendDto.SelectApplyInfoReq req = new FriendDto.SelectApplyInfoReq();
        req.setMeId(SharedData.me.getId());
        SharedData.client.sendMsg(Packet.create(FriendDto.SelectApplyInfoReq.class, req));
        Object obj = SharedData.future.get();
        if (obj instanceof FriendDto.SelectApplyInfoResp) {
            FriendDto.SelectApplyInfoResp resp = (FriendDto.SelectApplyInfoResp) obj;
            List<FriendDto.ApplyInfo> applyList = resp.getApplyList();
            for (FriendDto.ApplyInfo applyInfo : applyList) {
                beans.add(new NewFriendBean(applyInfo.getUser(), applyInfo.getInfo()));
            }
        }
        NewFriendAdapter adapter = new NewFriendAdapter(beans);
        newFriendList.setAdapter(adapter);

        toolbar.setNavigationIcon(R.drawable.icon_back);
        toolbar.setNavigationOnClickListener((view) -> finish());
    }
}
